package com.fansl.allround.message.handler;

import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.SocketIOClient;
import com.fansl.allround.common.core.util.R;
import com.fansl.allround.message.api.bean.ChatReceiveUser;
import com.fansl.allround.message.api.bean.ChatResponse;
import com.fansl.allround.message.api.constant.SocketConstant;
import com.fansl.allround.message.api.message.ChatMessage;
import com.fansl.allround.message.cache.ClientCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/9/5 10:59
 */
@Slf4j
@Component
public class ChatEventHandler {
    @Resource
    private ClientCache clientCache;

    /**
     * 处理群组消息
     * @param chatMessage
     * @return
     */
    public void handleGroupMessage(ChatMessage chatMessage){
        log.info("desc:{},param:{}","处理群组消息",JSONObject.toJSONString(chatMessage));

        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatType(chatMessage.getChatType());
        chatResponse.setUserId(chatMessage.getUserId());
        chatResponse.setUserName(chatMessage.getUserId()+"userName");
        chatResponse.setMessage(chatMessage.getMessage());

        List<HashMap<UUID, SocketIOClient>> allClient = clientCache.getAllUserClient();
        allClient.forEach(socketIOClientMap->{
            socketIOClientMap.forEach((uuid, socketIOClient)->{
                R result = R.<ChatResponse>builder().build().setData(chatResponse);
                socketIOClient.sendEvent(SocketConstant.SocketEvent.ChatEvent,result);
            });
        });
    }

    /**
     * 处理群组@个人消息
     * @param chatMessage
     * @return
     */
    public void handleGroupToSingleMessage(ChatMessage chatMessage){
        log.info("desc:{},param:{}","处理群组@个人消息",JSONObject.toJSONString(chatMessage));

        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatType(chatMessage.getChatType());
        chatResponse.setUserId(chatMessage.getUserId());
        chatResponse.setUserName(chatMessage.getUserId()+"userName");
        if(chatMessage.getReceiveUserIdList().size()==0){
            return;
        }
        List<ChatReceiveUser> receiveUserList = new ArrayList<>();
        chatMessage.getReceiveUserIdList().forEach(receiveUserId->{
            ChatReceiveUser chatReceiveUser = new ChatReceiveUser();
            chatReceiveUser.setUserId(receiveUserId);
            chatReceiveUser.setUserName(receiveUserId+"userName");
            receiveUserList.add(chatReceiveUser);
        });
        chatResponse.setReceiveUserList(receiveUserList);
        chatResponse.setMessage(chatMessage.getMessage());

        List<HashMap<UUID, SocketIOClient>> allClient = clientCache.getAllUserClient();
        allClient.forEach(socketIOClientMap->{
            socketIOClientMap.forEach((uuid, socketIOClient)->{
                R result = R.<ChatResponse>builder().build().setData(chatResponse);
                socketIOClient.sendEvent(SocketConstant.SocketEvent.ChatEvent,result);
            });
        });
    }

    /**
     * 处理单聊消息
     * @param chatMessage
     * @return
     */
    public void handleSingleMessage(ChatMessage chatMessage){
        log.info("desc:{},param:{}","处理单聊消息",JSONObject.toJSONString(chatMessage));

        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatType(chatMessage.getChatType());
        chatResponse.setUserId(chatMessage.getUserId());
        chatResponse.setUserName(chatMessage.getUserId()+"userName");
        if(chatMessage.getReceiveUserIdList().size()==0){
            return;
        }
        chatResponse.setMessage(chatMessage.getMessage());

        HashMap<UUID, SocketIOClient> clientMap =
                clientCache.getUserClient(chatMessage.getReceiveUserIdList().get(0));

        clientMap.forEach((uuid, socketIOClient)->{
            R result = R.<ChatResponse>builder().build().setData(chatResponse);
            socketIOClient.sendEvent(SocketConstant.SocketEvent.ChatEvent,result);
        });
    }
}
